package com.emser.unc.activitys;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.emser.unc.R;
import com.emser.unc.database.dao.ProductDataSource;
import com.emser.unc.database.dao.SaleOrderDataSource;
import com.emser.unc.serializable.ProductDTO;
import com.emser.unc.serializable.ProductSoldDTO;

public class ItemDetailActivity extends Activity implements OnClickListener{
	/** Called when the activity is first created. */
	

	private Button accept;
	private Button cancel;
	private TextView viewProductCode;
	private TextView viewProductTitle;
	private TextView viewProductStock;
	private TextView viewProductPrice;
	private ProductDataSource datasource;
	private SaleOrderDataSource saleDatasource;
	private ProductSoldDTO product;
	private EditText editText;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.m_item_detail);
		initWidgets();
		initDatasource();
		initDataView();
	}

	private void initWidgets() {
		accept = (Button) findViewById(R.id.buttonAccept);
		accept.setOnClickListener(this);
		cancel = (Button) findViewById(R.id.buttonCancel); 
		cancel.setOnClickListener(this);
		editText = (EditText)findViewById(R.id.editText1);
		viewProductCode = (TextView)findViewById(R.id.textView3);
		viewProductTitle = (TextView)findViewById(R.id.textView5);
		viewProductStock = (TextView)findViewById(R.id.textView7);
		viewProductPrice = (TextView)findViewById(R.id.textView9);
	}

	private void initDatasource() {
		datasource = new ProductDataSource(this);
		datasource.open();	
		saleDatasource = new SaleOrderDataSource(this);
		saleDatasource.open();
	}
	
	private void initDataView(){
		ProductDTO productDTO = datasource.getProduct(getIntent()
				.getLongExtra("productId", -1));
		viewProductCode.setText(productDTO.getCode().toString());
		viewProductTitle.setText(productDTO.getTitle());
		viewProductStock.setText(productDTO.getStock().toString());
		viewProductPrice.setText("$"+productDTO.getUnitPrice().toString());
		
		this.product = new ProductSoldDTO();
		this.product.setCode(productDTO.getCode());
		this.product.setTitle(productDTO.getTitle());
		this.product.setUnitPrice(productDTO.getUnitPrice());
		this.product.setStock(productDTO.getStock());

	}

	@Override
	protected void onResume() {
		datasource.open();
		saleDatasource.open();
		super.onResume();
	}

	@Override
	protected void onPause() {
		datasource.close();
		saleDatasource.close();
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindDrawables(findViewById(R.id.ItemDetailView));
	    System.gc();
	}
	
	private void unbindDrawables(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
		}
		if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				unbindDrawables(((ViewGroup) view).getChildAt(i));
			}
			((ViewGroup) view).removeAllViews();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()){
		case R.id.buttonAccept:
			if(!editText.getText().toString().matches("")&&!editText.getText().toString().equals("0")){
				
				DecimalFormatSymbols symbols = new DecimalFormatSymbols();
				symbols.setDecimalSeparator('.');
				DecimalFormat format = new DecimalFormat("#.##", symbols);
				
		
				product.setQuantity(Long.parseLong(editText.getText().toString()));
				product.setTotalPrice(Double.parseDouble(format.format(product.getUnitPrice()* product.getQuantity())));
				saleDatasource.createProductSold(product, getIntent()
						.getLongExtra("idReport", -1));
			}
			finish();
			break;
		case R.id.buttonCancel:

			finish();
			break;
		default:
			break;
		}
	}
}

